package com.css.project.infoInnovation.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.css.common.utils.file.FileUtils;
import com.css.framework.config.RuoYiConfig;
import org.apache.poi.ss.usermodel.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.css.framework.aspectj.lang.annotation.Log;
import com.css.framework.aspectj.lang.enums.BusinessType;
import com.css.framework.security.LoginUser;
import com.css.project.infoInnovation.domain.InformationInnovationSchedule;
import com.css.project.infoInnovation.service.IInformationInnovationScheduleService;
import com.css.framework.web.controller.BaseController;
import com.css.framework.web.domain.AjaxResult;
import com.css.common.utils.SecurityUtils;
import com.css.common.utils.poi.ExcelUtil;
import com.css.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 信创进度Controller
 * 
 * @author css
 * @date 2020-05-14
 */
@RestController
@RequestMapping("/infoInnovation/infoInnovation")
public class InformationInnovationScheduleController extends BaseController
{
    @Autowired
    private IInformationInnovationScheduleService informationInnovationScheduleService;

    /**
     * 查询信创进度列表
     */
    @PreAuthorize("@ss.hasPermi('infoInnovation:infoInnovation:list')")
    @GetMapping("/list")
    public TableDataInfo list(InformationInnovationSchedule informationInnovationSchedule)
    {
        startPage();
        List<InformationInnovationSchedule> list = informationInnovationScheduleService.selectInformationInnovationScheduleList(informationInnovationSchedule);
        return getDataTable(list);
    }

    /**
     * 导出信创进度列表
     */
    @PreAuthorize("@ss.hasPermi('infoInnovation:infoInnovation:export')")
    @Log(title = "信创进度", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InformationInnovationSchedule informationInnovationSchedule)
    {
        List<InformationInnovationSchedule> list = informationInnovationScheduleService.selectInformationInnovationScheduleList(informationInnovationSchedule);
        ExcelUtil<InformationInnovationSchedule> util = new ExcelUtil<InformationInnovationSchedule>(InformationInnovationSchedule.class);
        return util.exportExcel(list, "infoInnovation");
    }

    /**
     * 获取信创进度详细信息
     */
    @PreAuthorize("@ss.hasPermi('infoInnovation:infoInnovation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(informationInnovationScheduleService.selectInformationInnovationScheduleById(id));
    }

    /**
     * 新增信创进度
     */
    @PreAuthorize("@ss.hasPermi('infoInnovation:infoInnovation:add')")
    @Log(title = "信创进度", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InformationInnovationSchedule informationInnovationSchedule)
    {
        return toAjax(informationInnovationScheduleService.insertInformationInnovationSchedule(informationInnovationSchedule));
    }

    /**
     * 修改信创进度
     */
    @PreAuthorize("@ss.hasPermi('infoInnovation:infoInnovation:edit')")
    @Log(title = "信创进度", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InformationInnovationSchedule informationInnovationSchedule)
    {
        return toAjax(informationInnovationScheduleService.updateInformationInnovationSchedule(informationInnovationSchedule));
    }

    /**
     * 删除信创进度
     */
    @PreAuthorize("@ss.hasPermi('infoInnovation:infoInnovation:remove')")
    @Log(title = "信创进度", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(informationInnovationScheduleService.deleteInformationInnovationScheduleByIds(ids));
    }
    
    @GetMapping(value = "/getUser")
    public AjaxResult getUserInfo()
    {
        return AjaxResult.success(SecurityUtils.getLoginUser());
    }

    @PostMapping(value = "/uploadInfo")
    public AjaxResult uploadInfo (String filePath) {
        String newFilePath = RuoYiConfig.getUploadPath()+filePath.split("upload")[1];
        File file = new File(newFilePath);
        try{
            InputStream is = new FileInputStream(file);
            Workbook wb = WorkbookFactory.create(is);
            Sheet sheet = wb.getSheetAt(0);
            int num = 2;
            List<InformationInnovationSchedule> list = new ArrayList<InformationInnovationSchedule>();
            //校验数据
            for(int i=num; i <= sheet.getLastRowNum(); i++){
                Row row = sheet.getRow(i);
                String content = vaildData(row);
                if(!"".equals(content)){
                    FileUtils.deleteDir(newFilePath);
                    return AjaxResult.success("第"+(i+1)+"行数据"+content);
                }
            }
            //生成数据
            for(int i=num; i <= sheet.getLastRowNum(); i++){
                Row row = sheet.getRow(i);
                list.add(createModelByExcelRow(row));
            }
            if(list.size()>0){
                informationInnovationScheduleService.insertList(list);
            }
            FileUtils.deleteDir(newFilePath);
            return AjaxResult.success(1);
        }catch(Exception e){
            e.printStackTrace();
            FileUtils.deleteDir(newFilePath);
            return AjaxResult.success(2);
        }
    }
    private String vaildData(Row row){
        String content = "";
        Cell cell = row.getCell(0);
        try{
            new Date(ExcelUtil.getCellValue(cell));
        }catch(Exception e){
            content = "更新时间数据格式不正确!";
            return content;
        }
        Cell cell8 = row.getCell(8);
        try{
            Double.valueOf(ExcelUtil.getCellValue(cell8));
        }catch(Exception e){
            content = "总预算数据格式不正确!";
            return content;
        }
        Cell cell9 = row.getCell(9);
        try{
            Double.valueOf(ExcelUtil.getCellValue(cell9));
        }catch(Exception e){
            content = "目标金额数据格式不正确!";
            return content;
        }
        Cell cell11 = row.getCell(11);
        try{
            new Date(ExcelUtil.getCellValue(cell11));
        }catch(Exception e){
            content = "预计招标时间数据格式不正确!";
            return content;
        }
        Cell cell13 = row.getCell(13);
        try{
            Double.valueOf(ExcelUtil.getCellValue(cell13));
        }catch(Exception e){
            content = "成功率数据格式不正确!";
            return content;
        }
        return content;
    }
    private InformationInnovationSchedule createModelByExcelRow(Row row){
        InformationInnovationSchedule model = new InformationInnovationSchedule();
        Cell cell = row.getCell(0);
        model.setCreateTime(new Date(ExcelUtil.getCellValue(cell)));//创建时间
        Cell cell2 = row.getCell(2);
        model.setProjectName(ExcelUtil.getCellValue(cell2));//项目名称
        Cell cell3 = row.getCell(3);
        model.setProvArea(ExcelUtil.getCellValue(cell3));//省区
        Cell cell4 = row.getCell(4);
        model.setProvSelf(ExcelUtil.getCellValue(cell4));//省本级
        Cell cell5 = row.getCell(5);
        model.setMainCity(ExcelUtil.getCellValue(cell5));//省会及主要城市
        Cell cell6 = row.getCell(6);
        model.setKeyCompetitors(ExcelUtil.getCellValue(cell6));//主要竞争对手
        Cell cell7 = row.getCell(7);
        model.setSalesMan(ExcelUtil.getCellValue(cell7));//销售责任人
        Cell cell8 = row.getCell(8);
        model.setGeneralBudget(Double.valueOf(ExcelUtil.getCellValue(cell8)));//总预算
        Cell cell9 = row.getCell(9);
        model.setTargetAmount(Double.valueOf(ExcelUtil.getCellValue(cell9)));//目标金额
        Cell cell10 = row.getCell(10);
        model.setThreeYearSchedule(ExcelUtil.getCellValue(cell10));//三年进度安排
        Cell cell11 = row.getCell(11);
        model.setEstimatedBiddingTime(new Date(ExcelUtil.getCellValue(cell11)));//预计招标时间
        Cell cell12 = row.getCell(12);
        if("是".equals(ExcelUtil.getCellValue(cell12))){
            model.setProcurementMethod("1");//是=否分包
        }else{
            model.setProcurementMethod("2");//是=否分包
        }
        Cell cell13 = row.getCell(13);
        model.setSuccessRate(Double.valueOf(ExcelUtil.getCellValue(cell13)));//成功率
        Cell cell14 = row.getCell(14);
        if("自主".equals(ExcelUtil.getCellValue(cell14))){
            model.setProjectRunType("1");//项目运作方式
        }else{
            model.setProjectRunType("2");//项目运作方式
        }
        Cell cell15 = row.getCell(15);
        model.setSpecificProgress(ExcelUtil.getCellValue(cell15));//具体进展情况
        Cell cell16 = row.getCell(16);
        model.setProblem(ExcelUtil.getCellValue(cell16));//问题
        Cell cell17 = row.getCell(17);
        model.setSuggest(ExcelUtil.getCellValue(cell17));//建议
        Cell cell18 = row.getCell(18);
        if("已签".equals(ExcelUtil.getCellValue(cell18))){
            model.setSignStatus("1");//签约状态
        }else{
            model.setSignStatus("2");//签约状态
        }
        Cell cell19 = row.getCell(19);
        if("重点".equals(ExcelUtil.getCellValue(cell19))){
            model.setAttribute("1");//属性
        }else if("次重点".equals(ExcelUtil.getCellValue(cell19))){
            model.setAttribute("2");//属性
        }else if("一般".equals(ExcelUtil.getCellValue(cell19))){
            model.setAttribute("3");//属性
        }
        LoginUser cu = SecurityUtils.getLoginUser();
        model.setCreateBy(cu.getUsername());
        model.setUpdateTime(model.getCreateTime());
        model.setUpdateBy(cu.getUsername());
        return model;
    }
}
